package com.icongyou.enterprise.talent_marking.service.impl;

import com.icongyou.enterprise.common.result.Result;
import com.icongyou.enterprise.talent_marking.entity.dto.MarkRequest;
import com.icongyou.enterprise.talent_marking.entity.vo.StudentBasicInfo;
import com.icongyou.enterprise.talent_marking.entity.vo.StudentFocusMark;
import com.icongyou.enterprise.talent_marking.mapper.StudentFocusMarkMapper;
import com.icongyou.enterprise.talent_marking.service.StudentFocusMarkService;
import io.micrometer.common.util.StringUtils;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class StudentFocusMarkServiceImpl implements StudentFocusMarkService {
    @Resource
    private StudentFocusMarkMapper focusMarkMapper;

    /**
     * 标记关注学生
     */
    public Result markStudent(MarkRequest markRequest) {
        String userId = markRequest.getUserId();
        String studentId = markRequest.getStudentId();
        Integer tag = markRequest.getTag();
        // 1. 参数验证
        validateMarkParams(userId, studentId, tag);

        // 2. 检查是否已存在标记
        StudentFocusMark existingMark = focusMarkMapper.selectByUserAndStudent(userId, studentId);

        if (existingMark != null) {
            // 更新现有标记
            existingMark.setTag(tag);
            existingMark.setMarkedTime(new Date());
            focusMarkMapper.updateById(existingMark);
            return Result.success("标记已更新", existingMark);
        } else {
            // 创建新标记
            StudentFocusMark newMark = new StudentFocusMark();
            newMark.setId(UUID.randomUUID().toString());
            newMark.setUserId(userId);
            newMark.setStudentId(studentId);
            newMark.setTag(tag);
            newMark.setMarkedTime(new Date());

            focusMarkMapper.insert(newMark);
            return Result.success("标记已创建", newMark);
        }
    }

    /**
     * 取消关注
     */
    public Result unmarkStudent(String userId, String studentId) {
        focusMarkMapper.deleteByUserAndStudent(userId, studentId);
        return Result.success();
    }

    /**
     * 获取用户的所有关注学生
     */
    public Result getFocusStudentsByUser(String userId) {
        List<StudentFocusMark> studentFocusMarks = focusMarkMapper.selectByUserId(userId);
        return Result.success(studentFocusMarks);
    }

    /**
     * 检查是否已关注某学生
     */
    public Result isStudentFocusMarked(String userId, String studentId) {
        boolean isMark = focusMarkMapper.selectByUserAndStudent(userId, studentId) != null;
        return Result.success(isMark);
    }

    @Override
    public Result getFocusStudentInfo(String userId) {
        List<StudentBasicInfo> studentInfoList = focusMarkMapper.selectFocusStudentInfoByUserId(userId);
        if (studentInfoList == null || studentInfoList.isEmpty()) {
            return Result.success("暂无关注学生", studentInfoList);
        }
        return Result.success("获取关注学生成功", studentInfoList);
    }

    /**
     * 参数验证
     */
    private void validateMarkParams(String userId, String studentId, Integer tag) {
        if (StringUtils.isBlank(userId) || StringUtils.isBlank(studentId)) {
            throw new IllegalArgumentException("用户ID和学生ID不能为空");
        }

        if (tag == null || (tag != 0 && tag != 1)) {
            throw new IllegalArgumentException("标签值必须是0(一般关注)或1(重点关注)");
        }
    }
}
